<template>
  <div class="login">
    <div class="container">
      <div class="box">
        <div class="clock">
          <div class="hour">
            <div class="hr" id="hr" :style="transform_hr"></div>
          </div>
          <div class="min">
            <div class="mn" id="mn" :style="transform_mn"></div>
          </div>
          <div class="sec">
            <div class="sc" id="sc" :style="transform_sc"></div>
          </div>
          <div class="center">
            <div class="dynamic">
              <div class="anchor" style="transform: rotate(30deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="anchor" style="transform: rotate(60deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="minute-text" style="top: 0px; left: 135px">3</div>
              <div class="anchor" style="transform: rotate(120deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="anchor" style="transform: rotate(150deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="minute-text" style="top: 135px; left: 0px">6</div>
              <div class="anchor" style="transform: rotate(210deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="anchor" style="transform: rotate(240deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="minute-text" style="top: 0px; left: -135px">9</div>
              <div class="anchor" style="transform: rotate(300deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="anchor" style="transform: rotate(330deg)">
                <div class="element minute-line"></div>
              </div>
              <div class="minute-text" style="top: -135px; left: 0px">12</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="login-body">
      <div class="login-container">
        <div class="head">
          <img
            class="logo"
            src="https://s.weituibao.com/1582958061265/mlogo.png"
          />
          <div class="name">
            <div class="title">新蜂商城</div>
            <div class="tips">Vue3.0 后台管理系统</div>
          </div>
        </div>
        <el-form
          label-position="top"
          :rules="rules"
          :model="ruleForm"
          ref="loginForm"
          class="login-form"
        >
          <el-form-item label="账号" prop="username">
            <el-input
              type="text"
              v-model.trim="ruleForm.username"
              clearable
              autocomplete="off"
            >
              <template #prefix>
                <el-icon
                  class="el-input__icon"
                  :size="16"
                  style="vertical-align: middle"
                  ><User
                /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model.trim="ruleForm.password"
              @keyup.enter="submitForm"
              autocomplete="off"
              ><template #prefix>
                <el-icon
                  class="el-input__icon"
                  :size="16"
                  style="vertical-align: middle"
                  ><Lock
                /></el-icon> </template
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div style="color: #333">登录表示您已同意<a>《服务条款》</a></div>
            <el-button style="width: 100%" type="primary" @click="submitForm"
              >立即登录</el-button
            >
            <el-checkbox v-model="checked" @change="!checked"
              >下次自动登录</el-checkbox
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "@/utils/axios";
import md5 from "js-md5";
import _ from "lodash";
import { User, Lock } from "@element-plus/icons";
import { onMounted, onUnmounted, reactive, ref, toRefs, watch } from "vue";
import {
  localSet,
  localGet,
  cookieSet,
  cookieGet,
  cookieRemove,
} from "@/utils";
export default {
  name: "Login",
  components: {
    User,
    Lock,
  },
  setup() {
    const loginForm = ref(null);
    const state = reactive({
      ruleForm: {
        username: "",
        password: "",
      },
      checked: true,
      rules: {
        username: [
          { required: "true", message: "账户不能为空", trigger: "blur" },
        ],
        password: [
          { required: "true", message: "密码不能为空", trigger: "blur" },
        ],
      },
      deg: 6,
      transform_hr: "",
      transform_mn: "",
      transform_sc: "",
      timer: null,
    });
    onMounted(() => {
      // 时钟timer
      state.timer = setInterval(() => {
        let day = new Date();
        let hh = day.getHours() * 30;
        let mm = day.getMinutes() * state.deg;
        let ss = day.getSeconds() * state.deg;
        console.log(hh, mm, ss);
        state.transform_hr = `transform: rotateZ(${hh + mm / 12}deg)`;
        state.transform_mn = `transform: rotateZ(${mm}deg)`;
        state.transform_sc = `transform: rotateZ(${ss}deg)`;
      }, 1000);
      // 获取缓存
      state.checked = localGet("rememberMe") > 0 ? true : false;
      if (state.checked) {
        const cName = cookieGet("userName");
        const cPassWord = cookieGet("passWord");
        state.ruleForm.username = cName;
        setTimeout(() => {
          state.ruleForm.password = cPassWord;
        }, 300);
      }
    });
    onUnmounted(() => {
      clearInterval(state.timer);
    });
    // 监听ruleForm.username变化 针对clearable处理
    watch(
      () => state.ruleForm.username,
      (newValue, oldValue) => {
        resetForm();
      },
      { deep: true }
    );
    const submitForm = async () => {
      loginForm.value.validate((valid) => {
        if (valid) {
          window.location.href = "/";
          localSet("rememberMe", state.checked ? 1 : -1);
          if (state.checked) {
            cookieSet("userName", state.ruleForm.username, 3600);
            cookieSet("passWord", state.ruleForm.password, 3600);
          } else {
            cookieRemove("userName");
            cookieRemove("passWord");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    };
    // 账号发生变更后重置密码
    const resetForm = _.debounce(function () {
      state.ruleForm.password = "";
      // loginForm.value.resetFields(); // 重置表单
    }, 200);
    return {
      ...toRefs(state),
      loginForm,
      submitForm,
      resetForm,
    };
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
}
.login-body {
  z-index: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  width: 100%;
  background-color: transparent;
  /* background-image: linear-gradient(25deg, #077f7c, #3aa693, #5ecfaa, #7ffac2); */
}
.login-container {
  width: 420px;
  height: 500px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}
.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}
.head img {
  width: 100px;
  height: 100px;
  margin-right: 20px;
}
.head .title {
  font-size: 28px;
  color: #1baeae;
  font-weight: bold;
}
.head .tips {
  font-size: 12px;
  color: #999;
}
.login-form {
  width: 70%;
  margin: 0 auto;
}
</style>
<style>
.el-form--label-top .el-form-item__label {
  padding: 0;
}
.login-form .el-form-item {
  margin-bottom: 12px;
}

/* clock */

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.login::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    45deg,
    #e91e63,
    #e91e63 50%,
    #ffc107 50%,
    #ffc107
  );
}

.login::after {
  content: "";
  position: absolute;
  top: -20px;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    160deg,
    #03a9f4,
    #03a9f4 50%,
    transparent 50%,
    transparent
  );
  animation: animate 5s ease-in-out infinite;
}

.container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
}

.box {
  position: relative;
  z-index: 1;
  width: 400px;
  height: 400px;
  backdrop-filter: blur(25px);
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.5);
  animation: animate 5s ease-in-out infinite;
  animation-delay: -2.5s;
}

.clock {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  bottom: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: radial-gradient(transparent, rgba(255, 255, 255, 0.2)),
    url(clock.png);
  background-size: cover;
  border-radius: 50%;
  backdrop-filter: blur(25px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-right: none;
  border-bottom: none;
  box-shadow: -10px -10px 20px rgba(255, 255, 255, 0.1),
    10px 10px 20px rgba(0, 0, 0, 0.1), 0px 40px 50px rgba(0, 0, 0, 0.2);
}

.clock::before {
  content: "";
  position: absolute;
  width: 15px;
  height: 15px;
  background: #fff;
  border-radius: 50%;
  z-index: 1000;
}

.hour,
.min,
.sec {
  position: absolute;
}

.hour,
.hr {
  width: 160px;
  height: 160px;
}

.min,
.mn {
  width: 190px;
  height: 190px;
}

.sec,
.sc {
  width: 230px;
  height: 230px;
}

.hr,
.mn,
.sc {
  display: flex;
  justify-content: center;
  position: absolute;
  border-radius: 50%;
}

.hr::before {
  content: "";
  position: absolute;
  width: 8px;
  height: 80px;
  background: #ff105e;
  z-index: 11;
  border-radius: 6px;
}

.mn::before {
  content: "";
  position: absolute;
  width: 4px;
  height: 90px;
  background: #fff;
  z-index: 12;
  border-radius: 6px;
}

.sc::before {
  content: "";
  position: absolute;
  width: 2px;
  height: 150px;
  background: #fff;
  z-index: 12;
  border-radius: 6px;
}

.anchor {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  transform: scale(1.08);
}

.minute-line {
  background: white;
  width: 1.8px;
  height: 9.5px;
  transform: translate(-50%, -100%) translateY(-131px);
  opacity: 0.75;
}

.element {
  position: absolute;
  top: 0;
  left: 0;
}

.minute-text {
  position: absolute;
  font: 18px Arial, sans-serif;
  color: white;
  font-weight: bold;
  transform: translate(-50%, -50%);
}

@keyframes animate {
  0%,
  100% {
    transform: translateY(10px);
  }

  50% {
    transform: translateY(-10px);
  }
}
</style>